Page({
    data: {
      childId: "",
      name: "",
      idCard: "",
      photo: "",
      sex: 0,
      age: "",
      school: "",
      schoolClass: "",
      phone: "",
      teacherName: "",
      teacherPhone: "",
    },
  
    onLoad(options) {
      this.setData({ childId: options.childId });
      this.fetchChildInfo();
    },
  
    fetchChildInfo() {
      wx.request({
        url: `""http://localhost:8081/parent/child/${this.data.childId}`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`,
          'content-type': 'application/json'
        },
        success: (res) => {
          console.log("📌 获取孩子信息:", res.data);
          if (res.statusCode === 200) {
            this.setData({
              name: res.data.name,
              idCard: res.data.idCard,
              photo: res.data.photo,
              sex: res.data.sex,
              age: res.data.age,
              school: res.data.school,
              schoolClass: res.data.schoolClass,
              phone: res.data.phone,
              teacherName: res.data.teacherName,
              teacherPhone: res.data.teacherPhone
            });
          } else {
            wx.showToast({ title: '获取失败', icon: 'none' });
          }
        }
      });
    },
  
    onInputChange(e) {
      const { field } = e.currentTarget.dataset;
      this.setData({ [field]: e.detail.value });
    },
  
    onGenderChange(e) {
      this.setData({ sex: e.detail.value });
    },
  
    uploadPhoto() {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          console.log("📌 选择的照片:", res.tempFilePaths[0]);
          this.setData({ photo: res.tempFilePaths[0] });
        }
      });
    },
  
    updateChild() {
      console.log("📌 提交修改信息:", this.data);
  
      wx.request({
        url: `http://localhost:8081/parent/child/update`,
        method: 'PUT',
        data: {
          childId: this.data.childId,
          name: this.data.name,
          idCard: this.data.idCard,
          photo: this.data.photo,
          sex: Number(this.data.sex),
          age: Number(this.data.age),
          school: this.data.school,
          schoolClass: this.data.schoolClass,
          phone: this.data.phone,
          teacherName: this.data.teacherName,
          teacherPhone: this.data.teacherPhone
        },
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`,
          'content-type': 'application/json'
        },
        success: (res) => {
          console.log("✅ 更新成功:", res);
          wx.showToast({ title: '更新成功', icon: 'success' });
          wx.navigateBack();
        }
      });
    }
  });
  